package com.gxa.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.BusinessException;
import com.gxa.pojo.Merchandise;
import com.gxa.service.MerchandiseService;
import com.gxa.utils.DataCheck;
import com.gxa.utils.JsonResult;
import io.swagger.annotations.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @program: saas
 * @description: 商品信息维护
 * @author: Tirion
 * @create: 2020-09-22 15:58
 **/
@Controller
@RequestMapping("/merch")
@CrossOrigin
@ControllerAdvice
@Api(value = "商品信息维护", tags = "商品信息维护的api")
public class MerchandiseController {

    @Resource
    private MerchandiseService merchandiseService;

    @GetMapping("/delete")
    @ResponseBody
    @ApiOperation(value = "删除", httpMethod = "get", response = String.class)
    public Map<String, Object> delete(@RequestParam @ApiParam(name = "id",
            value = "目标id", required = true) Integer id) {
        JsonResult jsonResult = new JsonResult();
        if (id == null) {
            jsonResult.setData("id不能为空");
            jsonResult.setCode("403");
            return jsonResult.getValues();
        }
        int i = merchandiseService.deleteByPrimaryKey(id);
        DataCheck.check(jsonResult, i);
        return jsonResult.getValues();
    }

    @GetMapping("/deleteBatch")
    @ResponseBody
    @ApiOperation(value = "批量删除", httpMethod = "get", response = String.class)
    public Map<String, Object> delete(@RequestParam @ApiParam(name = "list",
            value = "目标id集合", required = true) List<Integer> list) {
        JsonResult jsonResult = new JsonResult();
        if (list == null) {
            jsonResult.setData("id不能为空");
            jsonResult.setCode("403");
            return jsonResult.getValues();
        }
        Integer i = merchandiseService.deleteBatch(list);
        DataCheck.check(jsonResult, i);
        return jsonResult.getValues();
    }

    @PostMapping("/save")
    @ResponseBody
    @ApiOperation(value = "添加品牌", httpMethod = "post", response = String.class)
    public Map<String, Object> save(@RequestBody @ApiParam(name = "merchandise",
            value = "商品的pojo信息", required = true) Merchandise merchandise) {
        JsonResult jsonResult = new JsonResult();
        if (merchandise == null) {
            jsonResult.setData("用户数据异常");
            jsonResult.setCode("403");
            return jsonResult.getValues();
        }
        int i = merchandiseService.insertSelective(merchandise);
        DataCheck.check(jsonResult, i);
        return jsonResult.getValues();
    }

    @GetMapping("/idQuery")
    @ResponseBody
    @ApiOperation(value = "根据id查询", httpMethod = "get", response = String.class)
    public Map<String, Object> findById(@RequestParam @ApiParam(name = "id",
            value = "目标id", required = true) Integer id) {
        JsonResult jsonResult = new JsonResult();
        if (id == null) {
            jsonResult.setData("id不能为空");
            jsonResult.setCode("403");
            return jsonResult.getValues();
        }
        Merchandise merchandise = merchandiseService.selectByPrimaryKey(id);
        DataCheck.check(jsonResult, merchandise);
        return jsonResult.getValues();
    }

    @PostMapping("/update")
    @ResponseBody
    @ExceptionHandler(BusinessException.class)
    @ApiOperation(value = "修改商品", httpMethod = "post", response = String.class)
    public Map<String, Object> update(@RequestBody @ApiParam(name = "merchandise",
            value = "商品的pojo信息", required = true) Merchandise merchandise) {
        JsonResult jsonResult = new JsonResult();
        if (merchandise != null) {
            int i = merchandiseService.updateByPrimaryKeySelective(merchandise);
            DataCheck.check(jsonResult, i);
        }
        return jsonResult.getValues();
    }


    @ResponseBody
    @PostMapping("/list")
    @ApiOperation(value = "查询", httpMethod = "post", response = String.class)
    @ApiImplicitParams({@ApiImplicitParam(name = "startIndex", value = "当前页",
             dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小",
                    dataType = "int", paramType = "query")
    })
    public Map<String, Object> list(@RequestParam(value = "startIndex", required = false, defaultValue = "0") Integer startIndex,
                                    @RequestParam(value = "pageSize", required = false, defaultValue = "0") Integer pageSize) {
        JsonResult jsonResult = new JsonResult();
        //分页插件
        PageHelper.startPage(startIndex, pageSize);
        //调用service
        List<Merchandise> merchandises = merchandiseService.queryAll();
        //检查结果是否异常
        DataCheck.check(jsonResult, merchandises);
        //通过pageInfo获取总记录数,添加到jsonResult
        PageInfo<Merchandise> merchandisePageInfo = new PageInfo<>(merchandises);
        long total = merchandisePageInfo.getTotal();
        jsonResult.put("total",total);
        return jsonResult.getValues();
    }

    @ResponseBody
    @PostMapping("/fuzzy")
    @ApiOperation(value = "模糊查询", httpMethod = "post", response = String.class)
    public Map<String, Object> fuzzyQuery(@RequestParam @ApiParam(name = "name",
            value = "商品名字", required = true) String name) {
        JsonResult jsonResult = new JsonResult();
        List<Merchandise> merchandises = merchandiseService.fuzzyQuery(name);
        DataCheck.check(jsonResult, merchandises);
        return jsonResult.getValues();
    }

    @GetMapping("/query")
    @ResponseBody
    @ApiOperation(value = "全查询", httpMethod = "get", response = String.class)
    public Map<String, Object> queryAll() {
        JsonResult jsonResult = new JsonResult();
        List<Merchandise> merchandises = merchandiseService.queryAll();
        DataCheck.check(jsonResult, merchandises);
        return jsonResult.getValues();
    }

    /**
     *
     * @param putaway 上架状态
     * @param id 目标id
     */
    @PostMapping("/putaway")
    @ResponseBody
    @ApiOperation(value = "上架状态", httpMethod = "post", response = String.class)
    @ApiImplicitParams({@ApiImplicitParam(name = "putaway", value = "是否上架",
            required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "id", value = "目标di", required = true,
                    dataType = "int", paramType = "query")
    })
    public Map<String, Object> updateStatus(@RequestParam("putaway") Integer putaway
            , @RequestParam("id") Integer id) {
        JsonResult jsonResult = new JsonResult();
        if (putaway != 0 && putaway != 1) {
            jsonResult.setData("状态有误");
            jsonResult.setCode("403");
            return jsonResult.getValues();
        }
        int i = merchandiseService.updateStatus(putaway, id);
        DataCheck.check(jsonResult, i);
        return jsonResult.getValues();
    }

    @GetMapping("/isPutaway")
    @ResponseBody
    @ApiOperation(value = "已上架",httpMethod = "get",response = String.class)
    public Map<String, Object> isPutaway() {
        JsonResult jsonResult = new JsonResult();
        List<Merchandise> putaway = merchandiseService.isPutaway();
        DataCheck.check(jsonResult,putaway);
        return jsonResult.getValues();
    }

    @GetMapping("/notPutaway")
    @ResponseBody
    @ApiOperation(value = "未上架",httpMethod = "get",response = String.class)
    public Map<String, Object> notPutaway() {
        JsonResult jsonResult = new JsonResult();
        List<Merchandise> putaway = merchandiseService.notPutaway();
        DataCheck.check(jsonResult,putaway);
        return jsonResult.getValues();
    }

    @PostMapping("/conjunctiveQuery")
    @ResponseBody
    @ApiOperation(value = "联合查询",httpMethod = "post",response = String.class)
    public Map<String, Object> conjunctiveQuery(@RequestBody Map<String,Object> map) {
        JsonResult jsonResult = new JsonResult();
        //获取分页条件
        String startIndex = map.get("StartIndex").toString();
        String pageSize = map.get("PageSize").toString();
        //分页插件
        PageHelper.startPage(Integer.parseInt(startIndex), Integer.parseInt(pageSize));
        //调用service
        List<Merchandise> merchandises = merchandiseService.conjunctiveQuery(map);
        PageInfo<Merchandise> pageInfo = new PageInfo<>(merchandises);
        long total = pageInfo.getTotal();
        jsonResult.put("total",total);
        DataCheck.check(jsonResult,merchandises);
        return jsonResult.getValues();
    }

    @GetMapping("/typeList")
    @ResponseBody
    @ApiOperation(value = "商品类型列表",httpMethod = "get",response = String.class)
    public Map<String, Object> typeList() {
        JsonResult jsonResult = new JsonResult();
        List<String> strings = merchandiseService.typeList();
        DataCheck.check(jsonResult,strings);
        return jsonResult.getValues();
    }

    @GetMapping("/brandList")
    @ResponseBody
    @ApiOperation(value = "品牌列表",httpMethod = "get",response = String.class)
    public Map<String, Object> brandList() {
        JsonResult jsonResult = new JsonResult();
        List<String> strings = merchandiseService.brandList();
        DataCheck.check(jsonResult,strings);
        return jsonResult.getValues();
    }
}
